package com.xxxx.server.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @Description 文件上传工具类
 * @Author shanxin
 * @Since 2021/6/10 16:57
 */
@Component
public class UploadFiles {

    private static String uploadUrl;
    @Value("${upload.url}")
    public void setUploadUrl(String url){
        uploadUrl = url;
    }
     /*private static Date data = new Date();
     private static SimpleDateFormat adf = new SimpleDateFormat("MM-dd");
     private static String format = adf.format(data);*/


    /**
     * TODO: 文件上传（可一次上传多个文件也可以一次上传一个文件）
     * @param files
     * @return
     */
    public static RespBean uploads(MultipartFile[] files) {
        //上传路径
        File targetFile = new File(uploadUrl);
        //如果路径不存在就创建
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        if (files.length > 0 && !(files[0].isEmpty())) {
            //创建流
            FileInputStream fis = null;
            FileOutputStream fos = null;
            //创建通道
            FileChannel inChannel = null;
            FileChannel outChannel = null;
            String newName;
            List<String> imgName = new ArrayList<>();
            try {
                for (MultipartFile file : files) {
                    String realName = file.getOriginalFilename();
                    //初始化文件新名字
                    if (realName != null && realName != "") {
                        //获取文件后缀
                        String suffixName = realName.substring(realName.lastIndexOf("."));
                        //生成新名字
                        newName = UUID.randomUUID().toString().replaceAll("-", "") + suffixName;
                    } else {
                        return RespBean.error("文件名不可为空");
                    }

                    fis = (FileInputStream) file.getInputStream();
                    //开始上传
                    fos = new FileOutputStream(uploadUrl + "\\" + newName);
                    //通道间传输
                    inChannel = fis.getChannel();
                    outChannel = fos.getChannel();
                    //上传
                    inChannel.transferTo(0, inChannel.size(), outChannel);
                    imgName.add(newName);
                }
                //打成压缩包
                //toCompress("haha");
            } catch (IOException e) {
                return RespBean.error("文件上传路径错误");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭资源
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    if (inChannel != null) {
                        inChannel.close();
                    }
                    if (outChannel != null) {
                        outChannel.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return RespBean.success("上传成功", uploadUrl+"\\"+imgName);
        }
        return RespBean.error("请选择文件");
    }

    /**
     * TODO: 文件上传（可一次上传一个文件）
     * @param file
     * @return
     */
    public static RespBean uploading(MultipartFile file) {
        //获取文件名
        String realName = file.getOriginalFilename();
        String newName = null;
        if (realName != null && realName != "") {
            //获取文件后缀
            String suffixName = realName.substring(realName.lastIndexOf("."));
            //生成新名字
            newName = UUID.randomUUID().toString().replaceAll("-", "") + suffixName;
        } else {
            return RespBean.error("文件名不可为空");
        }
        //创建流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        //创建通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = (FileInputStream) file.getInputStream();
            //开始上传
            fos = new FileOutputStream(uploadUrl + "\\" + newName);
            //通道间传输
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();
            //上传
            inChannel.transferTo(0, inChannel.size(), outChannel);

        } catch (IOException e) {
            return RespBean.error("文件上传路径错误");
        } finally {
            //关闭资源
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (inChannel != null) {
                    inChannel.close();
                }
                if (outChannel != null) {
                    outChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return RespBean.success("上传成功",uploadUrl+"\\"+newName);

    }

}

